package com.ordertracker.service;

import com.ordertracker.Application;
import com.ordertracker.Session;
import com.ordertracker.dao.GeneralCrudDao;
import com.ordertracker.exception.ValidationException;
import com.ordertracker.persistent.dictionary.Client;
import com.ordertracker.persistent.dictionary.Organization;
import com.ordertracker.persistent.dictionary.Position;
import com.ordertracker.protocol.CommandMessage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.ws.rs.core.Response;
import javax.xml.bind.JAXBException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Description of class.
 * Created 2/5/13 5:05 PM
 *
 * @author andrey.rodin@playtech.com
 */

@Service(value = "client")
public class ClientManager extends AbstractCRUDEventHandle<Client> {


    private GeneralCrudDao dao;

    @Resource(name = "crudDao")
    public void setDao(GeneralCrudDao<Client, Long> dao) {
        this.dao = dao;
    }


    @Override
    protected Client unmarshal(String obj) throws JAXBException {
        StringReader sr = new StringReader(obj);
        System.out.println("Position manager: create: "+sr);
        com.ordertracker.entity.Client client = Application.getApplication().getJsonUnmarshaller().unmarshalFromJSON(sr,
                    com.ordertracker.entity.Client.class);

        return transferObjectFactory.clientFromDTO(client);
    }

    @Override
    protected String getServiceName() {
        return "client";
    }

    @Override
    protected void validate(Session session, Client msg, ActionType type) throws ValidationException {

    }

    @Override
    protected Response otherAction(Session session, CommandMessage msg, Client obj, ActionType type) {
        //TODO: Here we can handle other actions  e.d. GET_ALL_LIST, GET_BY_CRITERIA and so on.
    	switch (type){
    	case GET_BY_CRITERIA:
    		String criteria = msg.getObj();
    		String[] cr = criteria.split(",");
    		
    		List<Client> list = dao.getAllByCriteria(cr[0], cr[1], Client.class);
    		
    		List<com.ordertracker.entity.Client> cliList = new ArrayList<com.ordertracker.entity.Client>();
    		
    		for (Client cli:list){
    			cliList.add(transferObjectFactory.clientToDTO(cli));
    		}  
    		
    		return Response.ok(cliList).build();
		default:
			return null;
    	}
    }

    @Override
    protected boolean needToUnmarshal(ActionType type) {
        switch (type) {
            case ADD:
            case UPDATE:
                return true;
            default:
                return false;
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    protected com.ordertracker.entity.Client create(Session session, Client obj) {
        Organization org = (Organization) dao.findById(obj.getOrganization().getId(), Organization.class);
        obj.setOrganization(org);
        Client client = (Client) dao.saveEntity(obj);
        return transferObjectFactory.clientToDTO(client);
    }

    @Override
    protected com.ordertracker.entity.Client receive(CommandMessage msg) {
        Long id = Long.valueOf(msg.getObj());

        return transferObjectFactory.clientToDTO((Client) dao.findById(id, Client.class));
    }

    @Override
    protected Object delete(Session session, Client obj) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    protected Object update(Session session, Client obj) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
